<template>
  <el-carousel class="carousel" height="800px">
    <el-carousel-item v-for="item in list" :name='item.id + ""' :key="item.id">
      <index-carousel-item :data="item" :direction="item.direction" :back-image="item.backImage"
      :title="item.title" :image="item.image" :link-good-id="item.linkGoodId" :link-good="item.linkGood"
      :goods="item.goods"/>
    </el-carousel-item>
  </el-carousel>
</template>

<script>
import IndexCarouselItem from '@/views/part/index/part/pageCarousel/IndexCarouselItem'
import { queryCarouselVO } from '@/api/ApiPageIndex'
import { ResultCode } from '@/constants/constants'
export default {
  name: 'PageCarousel',
  components: { IndexCarouselItem },
  data: () => {
    return {
      list: [],
      queryLoading: false
    }
  },
  mounted () {
    this.queryData()
  },
  methods: {
    /**
     * 转换数据
     */
    convertData (item) {
      if (item) {
        // linkGood中的price
        const linkGood = item['linkGood']
        if (linkGood) {
          const subGoods = linkGood['subGoodList']
          if (subGoods && typeof subGoods === 'object' && subGoods.length > 0) {
            linkGood.price = -1
            subGoods.forEach(tempItem => {
              if (tempItem && (linkGood.price === -1 || (tempItem['price'] && tempItem['price'] < linkGood.price))) {
                linkGood.price = tempItem.price
              }
            })
          }
        }
        // goods中的price和image
        const goods = item['goods']
        if (goods && typeof goods === 'object' && goods.length > 0) {
          goods.forEach(temp => {
            if (temp) {
              // price
              const subGoods = temp['subGoodList']
              if (subGoods && typeof subGoods === 'object' && subGoods.length > 0) {
                temp.price = -1
                subGoods.forEach(tempItem => {
                  if (tempItem && (temp.price === -1 || (tempItem['price'] && tempItem['price'] < temp.price))) {
                    temp.price = tempItem.price
                  }
                })
              }
              // image
              const images = temp['goodPictureList']
              if (images && typeof images === 'object' && images.length > 0) {
                temp.image = images[0].image
                temp.imageFileId = images[0].imageFileId
              }
            }
          })
        }
      }
      return item
    },
    queryData () {
      if (this.queryLoading) {
        return
      }
      this.queryLoading = true
      queryCarouselVO(false, data => {
        if (data && data.code === ResultCode.OK) {
          data = data.object
          const temp = []
          if (data && typeof data === 'object') {
            data.forEach(item => {
              temp.push(this.convertData(item))
            })
          }
          this.list = temp
        }
      }, () => {
        this.queryLoading = false
      })
    }
  }
}
</script>

<style lang='scss' scoped>

  .carousel{
    width: 100%;
    user-select: none;
  }

</style>
